#include "serialportservice.h"

SerialPortService::SerialPortService(QObject *parent)
    : QObject{parent}
{
    qDebug() << __FUNCTION__ << "Thread ID:" << QThread::currentThreadId();
    is_open = false;

}

SerialPortService::~SerialPortService()
{
    qDebug() << __FUNCTION__ << "Thread ID:" << QThread::currentThreadId();
    if( serial_port!=NULL)
        serial_port->deleteLater();
    if( serial_rec_timer!=NULL )
        serial_rec_timer->deleteLater();
}

void SerialPortService::SetSerialInfo(QString serial_name, QSerialPort::BaudRate serial_baud)
{
    qDebug() << __FUNCTION__ << "Thread ID:" << QThread::currentThreadId();

    this->serial_name = serial_name;
    this->serial_baud = serial_baud;
}

void SerialPortService::slots_OpenSerialPort()
{
    qDebug() << __FUNCTION__ << "Thread ID:" << QThread::currentThreadId();
    if( serial_port==NULL ) {
        qDebug() << __FUNCTION__ << "new serial port";
        serial_port = new QSerialPort;
        serial_rec_timer = new QTimer;
        //如果该函数在依附线程（主线程）中运行，就放到slots_OpenSerialPort()函数中进行连接
        connect(serial_port,&QSerialPort::errorOccurred,this,[=](){
            qDebug() << __FUNCTION__ << "Thread ID:" << QThread::currentThreadId();
            qDebug() << "serial port error:" << serial_port->errorString();
            if( serial_port->isOpen() ) {
                serial_port->close();
                is_open = false;
            }
        });
        //准备接收
        connect(serial_port,&QSerialPort::readyRead,this,[=](){
            qDebug() << __FUNCTION__ << "Thread ID:" << QThread::currentThreadId();
            serial_rec_data +=serial_port->readAll();
            serial_rec_timer->start(30); //30ms后没有接收到表示接收完毕
            qDebug() << __FUNCTION__ << "serial port receive: " << serial_rec_data;
        });
        //接收完成
        connect(serial_rec_timer,&QTimer::timeout,this,[=](){
            qDebug() << __FUNCTION__ << "Thread ID:" << QThread::currentThreadId();
            serial_rec_timer->stop();
            emit sig_SerialReceiveData(serial_rec_data);
            serial_rec_data.clear();
        });

    }
    if( serial_port->isOpen() ) {
        return;
    }
    serial_port->setPortName(serial_name);
    serial_port->setBaudRate(serial_baud);
    qDebug() << "serial name:" << serial_name;
    is_open = serial_port->open(QSerialPort::ReadWrite);
    if( is_open ) {
        qDebug() << "serial port open success";
    }
    else {
        qDebug() << "serial port open failed" << serial_port->errorString();
    }
}

void SerialPortService::slots_CloseSerialPort()
{
    qDebug() << __FUNCTION__ << "Thread ID:" << QThread::currentThreadId();
    if( serial_port->isOpen() ) {
        serial_port->close();
    }
    is_open = false;
}

void SerialPortService::slots_SerialWriteData(const QByteArray &data)
{
    qDebug() << __FUNCTION__ << "Thread ID:" << QThread::currentThreadId();
    //serial_port放在前面防止程序崩溃
    if( serial_port==NULL||!serial_port->isOpen() ){
        slots_OpenSerialPort();
        if( !serial_port->isOpen() ){
            return;
        }
    }
    serial_port->write(data);
}

void SerialPortService::slots_serialReadData(const QByteArray &data)
{

}







